#!/usr/bin/python
# -*- coding: utf-8 -*-
#Under the terms of the General Public License v.3
#For more information see LICENSE.txt

import pygame
from pygame.locals import K_LEFT, K_RIGHT, K_UP, K_DOWN, QUIT, KEYDOWN

import Player

WIDTH = 640
HIGTH = 480

class Map(object):


	def __init__(self):
		#This initiate the matris of the map
		self.matris = self.loadMatris ('./data/maps/Map.txt')
		self.blocks = self.loadBlockImage()
		self.backgrown = self.newBack()
		self.drawBack()
		self.camera_x = 0
		self.camera_y = 0
		self.destination_camera_x = 0
		self.destination_camera_y = 0

	def loadMatris(self, path):
		#this funtion return de content of Map.txt
		fileMap = file(path,'rt')
		content = fileMap.readlines()
		fileMap.close()
		return content

	def loadBlockImage(self):
		#this funtion return a list of all block image of de map
		blocks = []
		for n in range(3):
			blocks.append(pygame.image.load('./data/image/%d.png' %n).convert_alpha())
		return blocks

	def newBack(self):
		#this funtion return a surface size
		arrows = len(self.matris)
		columns =  (len(self.matris[0]) - 1)
		width = columns * 48
		higth = arrows * 48
		return pygame.Surface( (width, higth) ).convert()

	def drawBack(self):
		numberOfArrows = len(self.matris)
		numberOfColumns = (len(self.matris[0]) - 1)

		self.backgrown.fill((150, 150, 255))
		for arrow in range(numberOfArrows):
			for column in range(numberOfColumns):
				index = int(self.matris[arrow][column])
				pos_x = column * 48
				pos_y = arrow * 48
				self.backgrown.blit(self.blocks[index], (pos_x, pos_y))

	def show(self, screen):
		origin = (self.camera_x, self.camera_y, WIDTH, HIGTH)
		destination = (0, 0, WIDTH, HIGTH)
		screen.blit(self.backgrown, destination, origin)

	def move(self, dx, dy):
		self.destination_camera_x += dx
		self.destination_camera_y += dy
		self.limits()

	def update(self):
		self.camera_x += (self.destination_camera_x - self.camera_x) / 10.0
		self.camera_y += (self.destination_camera_y - self.camera_y) / 10.0

	def limits(self):
		if self.destination_camera_x < 0:
			self.destination_camera_x = 0

		if self.destination_camera_y < 0:
			self.destination_camera_y = 0

		if self.destination_camera_x + WIDTH > self.backgrown.get_width():
			self.destination_camera_x = self.backgrown.get_width() - WIDTH

		if self.destination_camera_y + HIGTH > self.backgrown.get_height():
			self.destination_camera_y = self.backgrown.get_height() - HIGTH

	def lookAt(self, rect):
		self.destination_camera_x = rect.x - WIDTH / 2
		self.destination_camera_y = rect.y - HIGTH / 2
		self.limits()

	def cantStep(self, x, y):
		arrow = y / 48
		column = x / 48
				
		try:
		    place = int(self.matris[arrow-1][column-1])
		except:
		    return False
		if place in [0, 1]:
		    return True
		return False

		pass


if __name__ == '__main__':

	def main():
		screen = pygame.display.set_mode((320, 240))
		pygame.display.set_caption("Map Module Test - Robot Game")

		map = Map()
		player = Player(map)
		exit_ = False

		while not exit_:

			for e in pygame.event.get():
				if e.type == QUIT:
					exit_ = True
				elif e.type == KEYDOWN and e.unicode == 'q':
					exit_ = True

			player.update()
			map.update()
			map.show(screen)
			pygame.display.flip()
			pygame.time.wait(10)

		pygame.display.init()

	main()
